package com.example.servlet;


import com.alibaba.fastjson.JSON;
import com.example.dao.CategoryDao;
import com.example.dao.imp.CategoryDaoImp;
import com.example.domain.Category;
import com.example.domain.Users;
import com.example.utils.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "CategoryServlet", value = "/categoryServlet")
public class CategoryServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String categoryName = request.getParameter("categoryName");
        CategoryDao categoryDao = new CategoryDaoImp();
        if (categoryName == null ) {
            List<Category> categoryList = categoryDao.getAllCategories();
            // 将分类列表转换为 JSON 格式并写入响应
            ResponseUtil.sendSuccessResponse(response, 200, categoryList);
        } else {//按分类名搜索
            List<Category> categoryList = categoryDao.searchCategoriesByName(categoryName);
            ResponseUtil.sendSuccessResponse(response, 200, categoryList);
        }

    }

    //增加操作
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 读取请求体中的 JSON 数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        reader.close();
        // 解析 JSON 数据并转换为 Category 对象
        Category category = JSON.parseObject(jsonBuilder.toString(), Category.class);
        CategoryDao categoryDao = new CategoryDaoImp();
        categoryDao.addCategory(category);
        ResponseUtil.sendSuccessResponse(response, 200, "分类添加成功");
    }

    //修改操作
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 读取请求体中的 JSON 数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        reader.close();
        // 解析 JSON 数据并转换为 Category 对象
        Category category = JSON.parseObject(jsonBuilder.toString(), Category.class);
        CategoryDao categoryDao = new CategoryDaoImp();
        categoryDao.updateCategory(category);
        ResponseUtil.sendSuccessResponse(response, 200, "分类修改成功");

    }

    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        if (id != null) {
            int categoryId = Integer.parseInt(id);
            CategoryDao categoryDao = new CategoryDaoImp();
            categoryDao.deleteCategory(categoryId);
            ResponseUtil.sendSuccessResponse(response, 200, "分类删除成功");
        } else {
            ResponseUtil.sendErrorResponse(response, 400, "分类ID不能为空");
        }
    }
}
